package com.chenjun.core.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.auth.service.AuthService;
import com.chenjun.core.dao.generator.entity.SysRoleResource;
import com.chenjun.core.dao.generator.mapper.SysRoleResourceMapper;
import com.chenjun.core.dao.module.SysRoleResourceRelationMapper;
import com.chenjun.core.service.sys.SysRoleResourceService;
import com.chenjun.core.vo.sys.RoleResourceVo;
@SuppressWarnings("serial")
@Service
@Transactional
public class SysRoleResourceServiceImpl implements SysRoleResourceService {
	private static final String ROLE_RESOURCE_MAPPER = "RoleResourceMapper";
    private static final Logger logger = Logger.getLogger(SysRoleResourceServiceImpl.class);

    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;
    @Autowired
    private SysRoleResourceRelationMapper sysRoleResourceRelationMapper;
    
	@Autowired
	private AuthService			authService;

	public SysRoleResource get(String id){
		return sysRoleResourceMapper.selectByPrimaryKey(id);
	}

	public int create(SysRoleResource record){
		return sysRoleResourceMapper.insert(record);
	}

	public int delete(String id){
		return sysRoleResourceMapper.deleteByPrimaryKey(id);
	}
	
	public int deleteByCondition(Map<String,String> params){
		return sysRoleResourceRelationMapper.deleteByCondition(params);
	}

	public int update(SysRoleResource record){
		return sysRoleResourceMapper.updateByPrimaryKey(record);
	}

	public int createOrUpdate(SysRoleResource record){
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			rows = sysRoleResourceMapper.insert(record);
		} else {
			rows = sysRoleResourceMapper.updateByPrimaryKeySelective(record);
		}
		return rows;
	}

	public int doCountByCondition(DataGridModel param){
		return sysRoleResourceRelationMapper.countByCondition(param);
	}

	public List<SysRoleResource> doListByCondition(DataGridModel param){
		return sysRoleResourceRelationMapper.selectByCondition(param);
	}

	public Pagination doPagination(DataGridModel param){
		Pagination vo = new Pagination();
		List<SysRoleResource> list = sysRoleResourceRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysRoleResourceRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}
	
	/**
	 * 
	 * 批量新增和删除列表
	 * @param bean
	 * @return
	 * int
	 */
	public int batchCreateOrUpdate(RoleResourceVo bean){
		int rows = 0;
		String roleId = bean.getRoleId();
		if(StringUtils.isNotEmpty(roleId)){
			
			Map<String,String> params = new HashMap<String, String>(1);
			params.put("roleId", roleId);
			rows = sysRoleResourceRelationMapper.deleteByCondition(params);
			
			List<String> resources = bean.getResources();
			if (!CollectionUtils.isEmpty(resources)) {
				List<SysRoleResource> rrs = new ArrayList<SysRoleResource>(resources.size());
				for (String resourceId : resources) {
					SysRoleResource rr = new SysRoleResource();
					rr.setResourceId(resourceId);
					rr.setRoleId(roleId);
					rrs.add(rr);
				}
				rows = sysRoleResourceRelationMapper.insertBatch(rrs);
			}
		}
		authService.reCreateFilterChains();
		return rows;
	}
}
